<?php

//decode by http://www.yunlu99.com/
function config()
{
	global $_W;
	$sql = 'SELECT * FROM ' . tablename('d1sj_card_setting') . ' WHERE uniacid = :uniacid';
	$res = pdo_fetch($sql, [':uniacid' => uniacid()]);
	$config = unserialize($res['config']);
	$config['invite'] = $res['invite'];
	$config['raiders'] = $res['raiders'];
	return $config;
}
function user($uid = null)
{
	global $_W;
	$sql = 'SELECT * FROM ' . tablename('d1sj_card_user') . ' WHERE uid = :id';
	$user = pdo_fetch($sql, [':id' => $uid ?: uid()]);
	return $user;
}
function input($key = null, $default = null)
{
	global $_GPC;
	if ($key) {
		return isset($_GPC[$key]) ? $_GPC[$key] : $default;
	}
	return $_GPC;
}
function is_post()
{
	return !empty($_POST);
}
function uid()
{
	global $_W;
	return $_W['fans']['uid'];
}
function uniacid()
{
	global $_W;
	return $_W['uniacid'];
}
function return_json($data, $code = 1)
{
	die(json_encode($data, JSON_UNESCAPED_UNICODE));
}
function get_page_total($sql, $params = array())
{
	$total = pdo_fetchcolumn($sql, $params);
	return $total;
}
function get_page($sql, $params = array(), $size = 10)
{
	$total = pdo_fetchcolumn($sql, $params);
	$index = max(1, input('page'));
	$size = $size;
	$pager = pagination($total, $index, $size);
	return $pager;
}
function get_limit($size = 10)
{
	$index = max(1, input('page'));
	return 'limit ' . ($index - 1) * $size . ',' . $size;
}
function get_order_sn()
{
	return random(24);
}
function get_group_info_by_id($id, $isUser = false)
{
	if ($isUser) {
		$user = user($id);
		if ($user['group']) {
			$id = $user['group'];
		} else {
			return false;
		}
	}
	$sql = 'SELECT * FROM ' . tablename('d1sj_card_group') . ' WHERE id = :id';
	$info = pdo_fetch($sql, [':id' => $id]);
	return $info;
}
function mobile_url($moduleName, $do, $query = array(), $noredirect = true)
{
	global $_W;
	$query['do'] = $do;
	$query['m'] = strtolower($moduleName);
	return murl('entry', $query, $noredirect, true);
}
function check_auth()
{
	global $_W;
	if (empty($_W['fans']['nickname'])) {
		mc_oauth_userinfo();
	}
	if (!pdo_exists('d1sj_card_user', ['uid' => uid(), 'uniacid' => uniacid()]) && uid()) {
		$cc = input('cc', 0);
		$data = ['uid' => uid(), 'uniacid' => uniacid(), 'leader1' => $cc];
		if ($cc) {
			$upUser = pdo_get('d1sj_card_user', ['uid' => $cc, 'uniacid' => uniacid()], '*');
			if ($upUser['leader1']) {
				$data['leader2'] = $upUser['leader1'];
			}
			if ($upUser['leader2']) {
				$data['leader3'] = $upUser['leader2'];
			}
		}
		pdo_insert('d1sj_card_user', $data);
	}
}
function figure_money($money)
{
	if ($money < 1000) {
		return $money / 100 . '百';
	} else {
		if ($money < 10000) {
			return $money / 1000 . '千';
		} else {
			return $money / 10000 . '万';
		}
	}
}
function Send($data)
{
	global $_W;
	$setting = pdo_fetch('select * from ' . tablename('d1sj_card_setting') . " where uniacid=:uniacid", array(':uniacid' => $_W['uniacid']));
	$configs = unserialize($setting['config']);
	$keyInfo = tempnam('', '') . '.pem';
	file_put_contents($keyInfo, $configs['apiclient_key']);
	$cretInfo = tempnam('', '') . '.pem';
	file_put_contents($cretInfo, $configs['apiclient_cert']);
	$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
	$post_data = make_send_data($data);
	$pem = [CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => true, CURLOPT_SSLCERTTYPE => 'PEM', CURLOPT_SSLCERT => $cretInfo, CURLOPT_SSLCERTPASSWD => $configs['mchid'], CURLOPT_SSLKEY => $keyInfo];
	$result = curl($url, $post_data, $pem);
	$result = FromXml($result);
	return $result;
}
function make_send_data($config)
{
	if (!$config['openid'] || !$config['money']) {
		return false;
	}
	$data = ['mch_appid' => $config['mch_appid'], 'mchid' => $config['mchid'], 'nonce_str' => make_str(32, 'int'), 'partner_trade_no' => make_billno($config['key']), 'openid' => $config['openid'], 'check_name' => 'NO_CHECK', 'amount' => $config['money'] * 100, 'desc' => '网贷超市', 'spbill_create_ip' => $_SERVER['SERVER_ADDR']];
	$data['sign'] = make_sign($data, $config['key']);
	$result = ToXml($data);
	return $result;
}
function make_billno($key)
{
	$number = 1234567890;
	$num = substr(str_shuffle(str_repeat($number, 5)), 0, 10);
	return date('Ymd') . $num;
}
function make_str($len = 10, $type = 'int')
{
	$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$int = 1234567890;
	switch ($type) {
		case 'int':
			return substr(str_shuffle(str_repeat($int, $len)), 0, $len);
		case 'string':
			return substr(str_shuffle(str_repeat($str . $int, $len)), 0, $len);
		default:
			return false;
	}
}
function make_sign($data, $key)
{
	ksort($data);
	$str = urldecode(http_build_query($data));
	$str = $str . '&key=' . $key;
	return strtoupper(md5($str));
}
function curl($url, $data = false, $s_option = array())
{
	$ch = curl_init();
	$option = [CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_SSL_VERIFYPEER => 0];
	if ($data) {
		$option[CURLOPT_POST] = 1;
		$option[CURLOPT_POSTFIELDS] = $data;
	}
	foreach ($s_option as $k => $v) {
		$option[$k] = $v;
	}
	curl_setopt_array($ch, $option);
	$response = curl_exec($ch);
	if (curl_errno($ch) > 0) {
		exit("CURL ERROR:$url " . curl_error($ch));
	}
	curl_close($ch);
	return $response;
}
function ToXml($data)
{
	$xml = "<xml>";
	foreach ($data as $key => $val) {
		if (is_numeric($val)) {
			$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
		} else {
			$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
		}
	}
	$xml .= "</xml>";
	return $xml;
}
function FromXml($xml)
{
	libxml_disable_entity_loader(true);
	return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
function get_banner_by_type($type)
{
	$types = ['news' => 1, 'lend' => 2, 'card' => 3];
	$sql = 'SELECT * FROM ' . tablename('d1sj_card_banner') . ' WHERE type = :type AND uniacid = :uniacid';
	return pdo_fetchall($sql, [':uniacid' => uniacid(), ':type' => $types[$type]]);
}